package com.thefriststage.module4.homework3;
/*
火车买票案例 (两种实现方式) :
共100张票,三个线程一起卖票,
不允许出现出现负票和多卖的现象  (描述设置锁的时候要注意的事项:(Thread 和Runnable)

作者：翁海龙
*/

//第一种方式：
public class TrainTicket1 implements Runnable {

    private int cnt=1;

    @Override
    public void run() {

        while (true){
            synchronized (this){
                notify();
                if(cnt<=100){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"正在卖第"+cnt+"票");
                    cnt++;
                    try {
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else{
                    break;
                }
            }

        }
}

    public static void main(String[] args) {
        TrainTicket1 tt=new TrainTicket1();
        Thread t1=new Thread(tt);
        Thread t2=new Thread(tt);
        Thread t3=new Thread(tt);
        t1.setName("窗口一");
        t2.setName("窗口二");
        t3.setName("窗口三");
        t1.start();
        t2.start();
        t3.start();
    }


}



